package hbec.kettle.etl.engine.tools;

import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import hbec.kettle.etl.common.constants.Constants;

public class JobConfGenDriver {
	
	public enum PluginType {
		Job, Trans, Fields;
	}
	
	private static Map<String, String> getPluginDirAsList(String path) {
		Map<String, String> ret = new HashMap<>();
		
		List<File> files = Arrays.asList(new File(path).listFiles());
		
		for (File file : files) {
			ret.put(file.getName().replaceAll("[.][^.]+$", ""), file.getAbsolutePath());
		}
		
		return ret;
	}
	
	private static Map<PluginType, Map<String, String>> filterPluginType() {
		Map<PluginType, Map<String, String>> ret = new HashMap<>();

		//ret.put(PluginType.Job, getPluginDirAsList(Constants.JOBDIR));
		//ret.put(PluginType.Trans, getPluginDirAsList(Constants.TRANSDIR));
		ret.put(PluginType.Fields, getPluginDirAsList(Constants.FIELDSDIR));

		return ret;
	}

	public static String produceXmlConf() {
		Map<PluginType, Map<String, String>> plugins = filterPluginType();
		
		Document document = DocumentHelper.createDocument();
		Element pluginsElement = document.addElement("plugins");
		/*
		for (Map.Entry<String, String> plugin : plugins.get(PluginType.Job).entrySet()) {
			Element jobElement = pluginsElement.addElement("job");
			jobElement.addAttribute("name", plugin.getKey());
			jobElement.addAttribute("path", plugin.getValue());
		}
		
		for (Map.Entry<String, String> plugin : plugins.get(PluginType.Trans).entrySet()) {
			Element tranElement = pluginsElement.addElement("trans");
			tranElement.addAttribute("name", plugin.getKey());
			tranElement.addAttribute("path", plugin.getValue());
		}
		*/
		for (Map.Entry<String, String> plugin : plugins.get(PluginType.Fields).entrySet()) {
			Element tranElement = pluginsElement.addElement("fields");
			tranElement.addAttribute("name", plugin.getKey());
			tranElement.addAttribute("path", plugin.getValue());
		}
		
		return document.asXML();
	}

}
